package com.qjdchina.sku.front.web.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qjdchina.common.web.util.RestBody;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.UserManager;
import com.qjdchina.sku.consts.WebConsts;
import com.qjdchina.sku.enums.UserType;
import com.qjdchina.sku.model.User;
import com.qjdchina.sku.model.vo.VerifyCodeVo;
import com.qjdchina.sku.service.enums.SkuResultCode;
import com.qjdchina.sku.util.SessionUtils;

/**
 * Created by fankiwu on 2016/11/16.
 */
@Controller
@RequestMapping(value = "user")
public class UserController {

    public static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserManager userManager;

    /**
     * 用户登录
     * @param username
     * @param password
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public RestBody<User> login(String username, String password,String smsVerifyCode, HttpSession httpSession){
    	//根据手机号判断是否是厂家 如果不是需要做短信验证
    	Result<User> result = userManager.getUserByUserName(username, password);
    	if(!result.isSuccess()){
    		return new RestBody<User>(SkuResultCode.USER_CANT_FIND);
    	}
    	User user = result.getDataModel();
    	String userType = user.getUserType();
    	if(!userType.equals(UserType.SUPPLIER.getValue())){
    		 // 验证短信
            VerifyCodeVo verifyCodeVo = SessionUtils.getSessionOfsVerifyCode(httpSession);
            // session里不存在 验证码失效
            if (verifyCodeVo == null) {
                return new RestBody<User>(SkuResultCode.SKU_FNT_VERIFY_CODE_FAILURE);
            }
            // 验证码错误
            if ((!smsVerifyCode.equals(verifyCodeVo.getVerifyCode()))
                    || (!username.equals(verifyCodeVo.getPhone()))) {
                return new RestBody<User>(SkuResultCode.SKU_FNT_VERIFY_CODE_NOT_EXISTS);
            }
    	}
    	
        Result<User> login = userManager.login(username, password, httpSession);
        if(!login.isSuccess()){
            LOGGER.warn("login fail !");
            return new RestBody<User>(login.getResultCode());
        }

        return new RestBody<User>(SkuResultCode.SUCCESS, login.getDataModel());
    }

    /**
     * 用户登出
     * @return
     */
    @RequestMapping(value = "logout",method = RequestMethod.POST)
    @ResponseBody
    public RestBody<String> logout(HttpSession httpSession){

        Enumeration<String> attributeNames = httpSession.getAttributeNames();
        while (attributeNames.hasMoreElements()){
            String s = attributeNames.nextElement();
            httpSession.removeAttribute(s);
        }

        return new RestBody<String>(SkuResultCode.SUCCESS, SkuResultCode.SUCCESS.getMessage());
    }
    
    /**
     * 
     * 重置密码
     * 2017年1月10日
     * @param username
     * @param password
     * @param newPassword
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "resetPassword",method = RequestMethod.POST)
    @ResponseBody
    public RestBody<String> resetPassword(String username, String password,String newPassword, HttpSession session){
    	if(StringUtils.isBlank(username)||StringUtils.isBlank(password)||StringUtils.isBlank(newPassword)){
    		return new RestBody<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
    	}
    	//校验手机号
    	Result<User> result = userManager.getUserByUserName(username, password);
    	if(!result.isSuccess()){
    		return new RestBody<String>(SkuResultCode.USER_CANT_FIND);
    	}
    	User user = result.getDataModel();
    	
    	Integer userId = (Integer) session.getAttribute(WebConsts.USER_ID);
		if(!userId.equals(user.getUserId())){
			return new RestBody<String>(SkuResultCode.NO_PERMISSION);
		}
		
    	user.setPassword(newPassword);
    	Result<String> update = userManager.update(user);
    	if(!update.isSuccess()){
    		return new RestBody<String>(update.getCode(),update.getMessage());
    	}
        return new RestBody<String>(SkuResultCode.SUCCESS);
    }
     
}
